var util = require('../../utils/util.js')
var editDialog=null;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    editMember: {
      editIndex: null,
      fromGroup:null,
      remark:null
    },
    memberList:[],
    searchText:"",
    paging: { height: 0, page: 1, limit: 10, totalPage: 0 },
    q:{}
  },
  nextPage: function () {
    var that = this;
    util.nextPage(function (param) {
      that.queryMember(that.data.q);
    });
  },
  queryMember:function(q){
    if(!q){
      q={};
    }
    util.pagingPost({
      uri: '/pub/useropenid/list',
      params: q,
      dataList: 'memberList',
      thatPage: this
    });
  },
  clearSearch: function () {
    this.setData({ searchText: "", "q": {} });
    this.queryMember();
  },
  search: function () {
    wx.navigateTo({
      url: "member-search"
    })
  },
  setSearchText: function (txt,q) {
    this.setData({ searchText: txt ,q:q});
  },
  edit(e){
    var index = e.currentTarget.dataset.index;
    var mem = { 
      "fromGroup": this.data.memberList[index].fromGroup, 
      "remark": this.data.memberList[index].remark, 
      "nickName": this.data.memberList[index].nickName, 
      "openid": this.data.memberList[index].openid,
      "editIndex": index
    };
   
    this.setData({ "editMember": mem});
    editDialog.showPopup();
  },
  //确认事件
  _success() {
    var mem = this.data.memberList[this.data.editMember.editIndex];
    if (this.data.editMember.fromGroup != mem.fromGroup || this.data.editMember.remark!=mem.remark){
      var that = this;
      var param = { 
        "fromGroup": this.data.editMember.fromGroup, 
        "remark": this.data.editMember.remark, 
        "openid": this.data.editMember.openid
      };
      util.post({ 
        uri: "/pub/useropenid/update", 
        params: param,
        success:function(r){
          mem.fromGroup = that.data.editMember.fromGroup;
          mem.remark = that.data.editMember.remark;
          var data={};
          data["memberList[" + that.data.editMember.editIndex + "]"] = mem;
          that.setData(data);
        }
      });
    }
    editDialog.hidePopup();
  },
  dataChange: function (e) {
    var name = e.currentTarget.dataset.name;
    var data = {};
    data[name] = e.detail.value;
    this.setData(data);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '会员管理',
    });

    var that = this;
    wx.getSystemInfo({
      success: (res) => {
        that.setData({
          "paging.height": res.windowHeight
        })
      }
    })
    this.queryMember();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    editDialog = this.selectComponent("#editDialog");
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})